<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function Page404()
{
	return '404';
}

function nav(array $arr_nav, $active='',$head='DNA LAB')
{
    $user = \think\Session::get('name');

    $nav_start = '
        <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">'.$head.'</a>
            </div>
            <div  id="navbar" class="navbar-collapse collapse">
                <ul class="nav navbar-nav">';
    $nav_end = '
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Hello,'.$user.'<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a id="exit">Exit</a></li>
                    </ul>
            </div>
        </div>
        </nav>';

    $nav_body = '';

    foreach ($arr_nav as $nav) {
        if (is_array($nav)) {
            $nav_body .= '<li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $nav['title'] . '<span class="caret"></span></a>
                <ul class="dropdown-menu">';
            foreach ($nav['content'] as $item) {
                $arr_str = preg_split('/,/', $item);
                if ($active == $nav['title'] .'.'. $arr_str[0]) {
                    $str_action = 'class="active"';
                    $link = '#';
                } else {
                    $str_action = '';
                    $link = $arr_str[1];
                }
                $nav_body .= '<li ' . $str_action . '><a href="' . $link . '">' . $arr_str[0] . '</a></li>';
            }
            $nav_body .= '</ul></li>';
        } else {
            $arr_str = preg_split('/,/', $nav);
            if ($active == $arr_str[0]) {
                $str_action = 'class="active"';
                $link = '#';
            } else {
                $str_action = '';
                $link = $arr_str[1];
            }
            $nav_body .= '<li ' . $str_action . '><a href="' . $link . '">' . $arr_str[0] . '</a></li>';
        }
        $nav_body.='<li class="divider"></li>';

    }
    return $nav_start.$nav_body.$nav_end;
}

function read_excel($filename)
{
    $info = pathinfo($filename);
    $type=$info['extension'];
    if($type=='xlsx'||$type=='xls' ){
        $reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 读取 excel 文档
    }else if( $type=='csv' ){
        $reader = \PHPExcel_IOFactory::createReader('CSV'); // 读取 excel 文档
    }else{
        die('Not supported file types!');
    }
    //载入文件
    $PHPExcel = $reader->load($filename);
    //获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
    $currentSheet = $PHPExcel->getSheet(0);
    //获取总列数
    $allColumn = $currentSheet->getHighestColumn();
    //获取总行数
    $allRow = $currentSheet->getHighestRow();
    //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
    for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
        //从哪列开始，A表示第一列
        for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
            //数据坐标
            $address = $currentColumn . $currentRow;
            //读取到的数据，保存到数组$arr中
            $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
        }
    }
    return ($data);
}

//把数字改写成123x10的X次方(上标)形式
function ciFang($data)
{
    $data = str_replace('e', 'E', $data);
    if (stripos($data, 'E')) {
        $a = strpos($data, 'E');
        $ciFang = substr($data, $a + 1);
        $CPI0 = substr($data, 0, 7);
        $b = substr($CPI0, 6, 1);
        $CPI_Fore = substr($CPI0, 0, 6);
        if ($b > 4) {
            $CPI_Fore = $CPI_Fore + 0.0001;
        }
    } else {
        if ($data > 1) {
            $CPI = sprintf("%.0f", $data);//四舍五入取整数
            $countCPI = strlen($CPI);//取长度
            if ($countCPI > 5) {
                $CPI0 = substr($data, 0, 6);
            } else {
                $a = substr($data, 0, $countCPI - 1);
                $b = substr($data, $countCPI + 1, 7 - $countCPI);
                $CPI0 = $a . $b;
            }
            $c = substr($CPI0, 5, 1);
            $CPI1 = substr($CPI0, 0, 5);
            if ($c > 4) {
                $CPI1++;
            }
            $CPI_Fore0 = substr($CPI1, 0, 1);
            $CPI_Fore1 = substr($CPI1, 1);
            $CPI_Fore = $CPI_Fore0 . '.' . $CPI_Fore1;
            $ciFang = $countCPI - 1;

        } else {
            for ($i = 0; $i < 40; $i++) {
                $j = substr($data, $i, 1);
                if ($j != 0) {
                    break;
                }
            }
            $CPI0 = substr($data, $i, 6);
            $c = substr($CPI0, 5, 1);
            $CPI1 = substr($CPI0, 0, 5);
            if ($c > 4) {
                $CPI1++;
            }
            $CPI_Fore0 = substr($CPI1, 0, 1);
            $CPI_Fore1 = substr($CPI1, 1);
            $CPI_Fore = $CPI_Fore0 . '.' . $CPI_Fore1;
            $ciFang = 1 - $i;

        }
    }
    return array($CPI_Fore, $ciFang);
}
